//
//  ShopListHeaderView.m
//  sensen_ios
//
//  Created by pro on 16/2/23.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopListHeaderView.h"

#import "ShopSortView.h"
#import "ShopFilterView.h"

@interface ShopListHeaderView () <ShopSortViewDelegate, ShopFilterViewDelegate>

@property (nonatomic, strong) UIView *firstView; // 综合排序
@property (nonatomic, strong) UIView *secondView; // 销量优先
@property (nonatomic, strong) UIView *thirdView; // 筛选

@property (nonatomic, strong) ShopSortView *sortView; // 筛选
@property (nonatomic, strong) ShopFilterView *filterView; // 筛选


@end

@implementation ShopListHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self _initViews];

    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    [self _createFirstView];
    [self _createSecondView];
    [self _createThirdView];
    
    // 顶部和底部各有一条分割线
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 1, self.width, 1)];
    topLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:topLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 0.5, self.width, 1)];
    bottomLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:bottomLine];
    
    // 创建竖线
    CGFloat x,y,width,height;
    for (int i = 1; i < 3; i ++) {
        width = 0.5;
        height = self.height - 16;
        x = (self.width / 3) * i;
        y = 8;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1];
        [self addSubview:line];
    }
}

// 创建综合排序视图
- (void)_createFirstView {
    
    CGFloat x, y, width, height;
    
    x = 0;
    y = 0;
    width = self.width / 3.0;
    height = 35;
    _firstView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:_firstView];
    _firstView.tag = 1;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_firstView addGestureRecognizer:tap];
    
    // 综合排序label
    // 设计尺寸
    NSString *title = @"综合排序";
    width = [title getSizeOfString:13].width;
    height = self.height;
    // 创建
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.userInteractionEnabled = YES;
    label.font = FONT(13);
    label.text = title;
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [_firstView addSubview:label];
    label.tag = 11;
    
    // 箭头
    // 设计尺寸
    height = 16;
    width = 16;
    // 创建
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    imgView.image = [UIImage imageNamed:@"筛选"];
    [_firstView addSubview:imgView];
    imgView.tag = 12;
    
    // 重排位置 (-2是箭头和label之间的间隙)
    label.left = (_firstView.width - label.width - imgView.width - 4) / 2;
    imgView.left = label.right + 4;
    imgView.center = CGPointMake(imgView.center.x, label.center.y);
}
- (void)_createSecondView {
    CGFloat x, y, width, height;
    
    x = self.width * 1 / 3.0;
    y = 0;
    width = self.width / 3.0;
    height = 35;
    _secondView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:_secondView];
    _secondView.tag = 2;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_secondView addGestureRecognizer:tap];
    
    // 销量优先label
    // 设计尺寸
    NSString *title = @"销量优先";
    width = [title getSizeOfString:13].width;
    height = self.height;
    // 创建
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.userInteractionEnabled = YES;
    label.font = FONT(13);
    label.text = title;
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [_secondView addSubview:label];
    label.tag = 11;
    
    label.center = CGPointMake(_secondView.width / 2, _secondView.height / 2);

}
// 创建筛选视图
- (void)_createThirdView {
    
    CGFloat x, y, width, height;
    
    x = self.width * 2 / 3.0;
    y = 0;
    width = self.width / 3.0;
    height = self.height;
    _thirdView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:_thirdView];
    _thirdView.tag = 3;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_thirdView addGestureRecognizer:tap];
    
    // 综合排序label
    // 设计尺寸
    NSString *title = @"筛选";
    width = [title getSizeOfString:13].width;
    height = self.height;
    // 创建
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.userInteractionEnabled = YES;
    label.font = FONT(13);
    label.text = title;
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [_thirdView addSubview:label];
    label.tag = 11;
    
    // 箭头
    // 设计尺寸
    height = 16;
    width = 16;
    // 创建
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    imgView.image = [UIImage imageNamed:@"箭头-下-灰"];
    [_thirdView addSubview:imgView];
    imgView.tag = 12;
    
    // 重排位置 (-2是箭头和label之间的间隙)
    label.left = (_thirdView.width - label.width - imgView.width - 4) / 2;
    imgView.left = label.right + 4;
    imgView.center = CGPointMake(imgView.center.x, label.center.y);
}

#pragma mark - 点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap {

    if ([self.delegate respondsToSelector:@selector(headerViewDidSelected:)]) {
        
        // 执行代理
        [self.delegate headerViewDidSelected:tap.view.tag];
    }
    
}


#pragma mark - 重写 set
- (void)setFirstViewTitle:(NSString *)firstViewTitle {
    
    _firstViewTitle = firstViewTitle;
    
    UILabel *titleLabel = (UILabel *)[_firstView viewWithTag:11];
    UIImageView *imgView = (UIImageView *)[_firstView viewWithTag:12];
    
    titleLabel.text = _firstViewTitle;
    if (![_firstViewTitle isEqualToString:@"排序"]) { // 点击过排序内容的情况
        titleLabel.textColor = SS_NAVI_ORANGE_COLOR;
        imgView.image = [UIImage imageNamed:@"箭头-下-红"];
    } else {
        [_sortView resetSortView];
        titleLabel.textColor = [UIColor darkGrayColor];
        imgView.image = [UIImage imageNamed:@"箭头-下-灰"];
    }
    titleLabel.width = [_firstViewTitle getSizeOfString:13].width;
    
    // 重排位置 (-2是箭头和label之间的间隙)
    titleLabel.left = (_thirdView.width - titleLabel.width - imgView.width - 4) / 2;
    imgView.left = titleLabel.right + 2;
}
- (void)setSecondTitleUse:(BOOL)secondTitleUse {
    
    _secondTitleUse = secondTitleUse;
    UILabel *titleLabel = (UILabel *)[_secondView viewWithTag:11];
    titleLabel.textColor = _secondTitleUse?SS_NAVI_ORANGE_COLOR:[UIColor darkGrayColor];
}
- (void)setThirdTitleUse:(BOOL)thirdTitleUse {
    
    _thirdTitleUse = thirdTitleUse;
    UILabel *titleLabel = (UILabel *)[_thirdView viewWithTag:11];
    UIImageView *imgView = (UIImageView *)[_thirdView viewWithTag:12];
    titleLabel.textColor = _thirdTitleUse?SS_NAVI_ORANGE_COLOR:[UIColor darkGrayColor];
    imgView.image = _thirdTitleUse?[UIImage imageNamed:@"箭头-下-红"]:[UIImage imageNamed:@"箭头-下-灰"];
}


#pragma mark - 点击了三个选项中的一个
- (void)tapView:(ZKHeaderType)type {
    
    switch (type) {
        case typeNone:
        {
            _type = typeNone;
            
            if (!_sortView.hidden) {
                [_sortView setHidden:YES];
            }
            if (!_filterView.hidden) {
                [_filterView setHidden:YES];
            }
        }
            break;
        case typeSort:
        {
            [self _tapSortView];
        }
            break;
        case typeFilter:
        {
            [self _tapFilterViews];
        }
            break;
        default:
            break;
    }
    
}
// 创建排序视图
- (void)_tapSortView {
    // 如果筛选视图存在就隐藏
    if (!_filterView.hidden) {
        [_filterView setHidden:YES];
    }
    
    // 第一次点击的时候要创建
    if (!_sortView) {
        _sortView = [[ShopSortView alloc] initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 120)];
        _sortView.delegate = self;
        [self addSubview:_sortView];
    } else {
        // 如果已经存在了根据是否隐藏来判断
        [_sortView setHidden:!_sortView.hidden];
    }
    
    _type = _sortView.hidden ? typeNone : typeSort;


}
// 创建筛选视图
- (void)_tapFilterViews {
    // 如果筛选视图存在就隐藏
    if (!_sortView.hidden) {
        [_sortView setHidden:YES];
    }
    
    // 第一次点击的时候要创建
    if (!_filterView) {
        _filterView = [[ShopFilterView alloc] initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 90)];
        _filterView.delegate = self;
        [self addSubview:_filterView];
    } else {
        // 如果已经存在了根据是否隐藏来判断
        [_filterView setHidden:!_filterView.hidden];
    }
    
    _type = _filterView.hidden ? typeNone : typeFilter;
    
}


#pragma mark - 排序和筛选的代理
- (void)sortViewDidChoose:(NSInteger)tag {
    
    self.secondTitleUse = NO;
    if ([self.delegate respondsToSelector:@selector(headerSortViewDidChoose:)]) {
        [self.delegate headerSortViewDidChoose:tag];
    }
}
- (void)filterViewDidChooseRange:(NSString *)filterRange {
    
    if (filterRange == nil) {
        self.thirdTitleUse = NO;
    } else {
        self.thirdTitleUse = YES;
    }
    if ([self.delegate respondsToSelector:@selector(headerFilterViewDidChooseRange:)]) {
        [self.delegate headerFilterViewDidChooseRange:filterRange];
    }
}
@end
